package com.lagou.demo.delayed.controller;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

@RestController
public class DelayController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @RequestMapping("/meeting/{second}")
    public String bookMeeting(@PathVariable Integer second){
        MessageProperties properties = MessagePropertiesBuilder.newInstance()
                //消息延时时间，到时间后才发送到绑定的队列中
                .setHeader("x-delayed", (second - 10) * 1000)
                .setContentEncoding("utf-8").build();

        Message message = MessageBuilder.withBody("还有10秒开始开会了".getBytes(StandardCharsets.UTF_8)).andProperties(properties).build();

        amqpTemplate.send("ex.delayed","key.delayed",message);

        return "会议订好了";

    }

}
